/********************************************************************************
 * Copyright(c) 2020-2025 VINCENT_WY All rights reserved.
 * - Filename
 * - Author  Vincent
 * - Version V1.0.0
 * - Date    2023/09/21
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */

#pragma once

#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "Message.hpp"
#include "DispatchFileReader.hpp"
#include "IReplayImpl.hpp"
#include "TimeUtil.h"

class NoBufferedReplayImpl : public IReplayImpl
{
public:
  NoBufferedReplayImpl(const int &id, const std::string &path);
  ~NoBufferedReplayImpl() override;
  void start(const std::function<void(std::shared_ptr<lpc::LidarProtoPointCloud> &&)> &, const long long int &time) override;
  void stop() override;
  bool isComplete() override;
  void doRepeat() override;
  uint64_t getFirstFrameTm() override;

private:
  enum class Status
  {
    START = 0,
    REPEAT,
    CONTINUE,
    IDLE
  };
  const std::string TAG;
  bool _isExit{false};
  bool _isComplete{false};
  int _frameId;
  uint64_t _startTime{0};
  std::string _root{};
  std::mutex _frameMtx;
  std::condition_variable _frameCv;
  std::vector<DispatchFileInfo> _fileInfoList;
  Status _status{Status::IDLE};
  std::unique_ptr<message::Context> _context{nullptr};
  std::shared_ptr<DispatchFileReader> _fileReaderPtr;
  std::deque<std::shared_ptr<lpc::LidarProtoPointCloud>> _frameDqu;
  std::shared_ptr<message::Publisher<lpc::LidarProtoPointCloud>> _lpcPointCloudPub;
  std::unique_ptr<std::thread> _readLoop{nullptr};
  std::unique_ptr<std::thread> _invokeLoop{nullptr};
  void ReadLoop();
  void InvokeLoop(const std::function<void(std::shared_ptr<lpc::LidarProtoPointCloud> &&)> &);
  TimeUtil::FpsCalc _fps{};
};